<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>Spring夺命连环10问 | 小狐档案库</title>
    <meta name="generator" content="VuePress 1.8.2">
    <link rel="icon" href="/favicon.ico">
    <meta name="description" content="学习资料">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/assets/css/0.styles.0c36f186.css" as="style"><link rel="preload" href="/assets/js/app.7029ddab.js" as="script"><link rel="preload" href="/assets/js/3.ebaa3085.js" as="script"><link rel="preload" href="/assets/js/1.8ce67e8c.js" as="script"><link rel="preload" href="/assets/js/61.0b61afdb.js" as="script"><link rel="prefetch" href="/assets/js/10.15c1f9c8.js"><link rel="prefetch" href="/assets/js/11.dc2fbddf.js"><link rel="prefetch" href="/assets/js/12.26ab1f36.js"><link rel="prefetch" href="/assets/js/13.6e1d4dbc.js"><link rel="prefetch" href="/assets/js/14.0cd48e80.js"><link rel="prefetch" href="/assets/js/15.74a9f029.js"><link rel="prefetch" href="/assets/js/16.24ef3382.js"><link rel="prefetch" href="/assets/js/17.c8a9bd0f.js"><link rel="prefetch" href="/assets/js/18.fbd494c2.js"><link rel="prefetch" href="/assets/js/19.f9a0df71.js"><link rel="prefetch" href="/assets/js/20.e6d0df38.js"><link rel="prefetch" href="/assets/js/21.932645cc.js"><link rel="prefetch" href="/assets/js/22.d6027dce.js"><link rel="prefetch" href="/assets/js/23.53bce141.js"><link rel="prefetch" href="/assets/js/24.ed3b8b64.js"><link rel="prefetch" href="/assets/js/25.ed88877e.js"><link rel="prefetch" href="/assets/js/26.f5ad24d5.js"><link rel="prefetch" href="/assets/js/27.64ad45b4.js"><link rel="prefetch" href="/assets/js/28.f308f8ab.js"><link rel="prefetch" href="/assets/js/29.4d95742d.js"><link rel="prefetch" href="/assets/js/30.0a867de0.js"><link rel="prefetch" href="/assets/js/31.f12def86.js"><link rel="prefetch" href="/assets/js/32.6b4fd8b7.js"><link rel="prefetch" href="/assets/js/33.4bb2aa9c.js"><link rel="prefetch" href="/assets/js/34.013e731c.js"><link rel="prefetch" href="/assets/js/35.6b263ef8.js"><link rel="prefetch" href="/assets/js/36.06acf3cc.js"><link rel="prefetch" href="/assets/js/37.fe72c29c.js"><link rel="prefetch" href="/assets/js/38.9b4e0bd6.js"><link rel="prefetch" href="/assets/js/39.0cde0408.js"><link rel="prefetch" href="/assets/js/4.9b5650bb.js"><link rel="prefetch" href="/assets/js/40.56bda9a8.js"><link rel="prefetch" href="/assets/js/41.007ae34c.js"><link rel="prefetch" href="/assets/js/42.bdaf414b.js"><link rel="prefetch" href="/assets/js/43.78d32d4e.js"><link rel="prefetch" href="/assets/js/44.a10cf8a5.js"><link rel="prefetch" href="/assets/js/45.19e14721.js"><link rel="prefetch" href="/assets/js/46.57ebcf06.js"><link rel="prefetch" href="/assets/js/47.9a513ffe.js"><link rel="prefetch" href="/assets/js/48.245b165a.js"><link rel="prefetch" href="/assets/js/49.fab43655.js"><link rel="prefetch" href="/assets/js/5.a16ef7f8.js"><link rel="prefetch" href="/assets/js/50.48c191fd.js"><link rel="prefetch" href="/assets/js/51.28a94b0e.js"><link rel="prefetch" href="/assets/js/52.e783fbf9.js"><link rel="prefetch" href="/assets/js/53.2f0e960e.js"><link rel="prefetch" href="/assets/js/54.577ff54e.js"><link rel="prefetch" href="/assets/js/55.4bb99598.js"><link rel="prefetch" href="/assets/js/56.e820e954.js"><link rel="prefetch" href="/assets/js/57.0cea0cfa.js"><link rel="prefetch" href="/assets/js/58.e31cfcd0.js"><link rel="prefetch" href="/assets/js/59.458ab168.js"><link rel="prefetch" href="/assets/js/6.7da424ba.js"><link rel="prefetch" href="/assets/js/60.e9caace2.js"><link rel="prefetch" href="/assets/js/62.c240acab.js"><link rel="prefetch" href="/assets/js/63.71cd2a97.js"><link rel="prefetch" href="/assets/js/64.977ea29b.js"><link rel="prefetch" href="/assets/js/65.79b2d62a.js"><link rel="prefetch" href="/assets/js/66.8da69071.js"><link rel="prefetch" href="/assets/js/67.fdc57d7e.js"><link rel="prefetch" href="/assets/js/68.d43eabe9.js"><link rel="prefetch" href="/assets/js/7.6e92edd1.js"><link rel="prefetch" href="/assets/js/8.2389170b.js"><link rel="prefetch" href="/assets/js/9.77841f24.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0c36f186.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container" data-v-1156296a><div data-v-1156296a><div id="loader-wrapper" class="loading-wrapper" data-v-d48f4d20 data-v-1156296a data-v-1156296a><div class="loader-main" data-v-d48f4d20><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div><div data-v-d48f4d20></div></div> <!----> <!----></div> <div class="password-shadow password-wrapper-out" style="display:none;" data-v-4e82dffc data-v-1156296a data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>小狐档案库</h3> <p class="description" data-v-4e82dffc data-v-4e82dffc>学习资料</p> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>小狐</span>
            
          <span data-v-4e82dffc>2021-9-1 - </span>
          2021
        </a></span></div></div> <div class="hide" data-v-1156296a><header class="navbar" data-v-1156296a><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><img src="/logo.png" alt="小狐档案库" class="logo"> <span class="site-name">小狐档案库</span></a> <div class="links"><div class="color-picker"><a class="color-button"><i class="iconfont reco-color"></i></a> <div class="color-picker-menu" style="display:none;"><div class="mode-options"><h4 class="title">Choose mode</h4> <ul class="color-mode-options"><li class="dark">dark</li><li class="auto active">auto</li><li class="light">light</li></ul></div></div></div> <div class="search-box"><i class="iconfont reco-search"></i> <input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/GO/" class="nav-link"><i class="undefined"></i>
  GO
</a></li><li class="dropdown-item"><!----> <a href="/categories/面试系列/" class="nav-link"><i class="undefined"></i>
  面试系列
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  足迹
</a></div><div class="nav-item"><a href="https://www.xiaohu.team" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-message"></i>
  摇光图库
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav></div></header> <div class="sidebar-mask" data-v-1156296a></div> <aside class="sidebar" data-v-1156296a><div class="personal-info-wrapper" data-v-828910c6 data-v-1156296a><img src="/avatar.jpg" alt="author-avatar" class="personal-img" data-v-828910c6> <h3 class="name" data-v-828910c6>
    小狐
  </h3> <div class="num" data-v-828910c6><div data-v-828910c6><h3 data-v-828910c6>58</h3> <h6 data-v-828910c6>Articles</h6></div> <div data-v-828910c6><h3 data-v-828910c6>20</h3> <h6 data-v-828910c6>Tags</h6></div></div> <ul class="social-links" data-v-828910c6></ul> <hr data-v-828910c6></div> <nav class="nav-links"><div class="nav-item"><a href="/" class="nav-link"><i class="iconfont reco-home"></i>
  首页
</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title"><i class="iconfont reco-category"></i>
      分类
    </span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/categories/GO/" class="nav-link"><i class="undefined"></i>
  GO
</a></li><li class="dropdown-item"><!----> <a href="/categories/面试系列/" class="nav-link"><i class="undefined"></i>
  面试系列
</a></li></ul></div></div><div class="nav-item"><a href="/tag/" class="nav-link"><i class="iconfont reco-tag"></i>
  Tag
</a></div><div class="nav-item"><a href="/timeline/" class="nav-link"><i class="iconfont reco-date"></i>
  足迹
</a></div><div class="nav-item"><a href="https://www.xiaohu.team" target="_blank" rel="noopener noreferrer" class="nav-link external"><i class="iconfont reco-message"></i>
  摇光图库
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></div> <!----></nav> <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>面试系列</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading open"><span>java</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><section class="sidebar-group collapsable is-sub-group depth-2"><p class="sidebar-heading open"><span>java基础系列</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blogs/audition/java/java_base/base_1.html" class="sidebar-link">java基础面试题一</a></li><li><a href="/blogs/audition/java/java_base/base_2.html" class="sidebar-link">java基础面试题二</a></li><li><a href="/blogs/audition/java/java_base/base_3.html" class="sidebar-link">java基础面试题三</a></li><li><a href="/blogs/audition/java/java_base/base_4.html" class="sidebar-link">java基础面试题四</a></li><li><a href="/blogs/audition/java/java_base/base_5.html" class="sidebar-link">java基础面试题四</a></li><li><a href="/blogs/audition/java/java_base/base_6.html" class="sidebar-link">java基础面试题六</a></li><li><a href="/blogs/audition/java/java_base/base_7.html" class="sidebar-link">java基础面试题七</a></li><li><a href="/blogs/audition/java/java_base/base_8.html" class="sidebar-link">java基础面试题八</a></li></ul></section></li><li><section class="sidebar-group collapsable is-sub-group depth-2"><p class="sidebar-heading"><span>并发编程系列</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-2"><p class="sidebar-heading"><span>Spring系列</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-2"><p class="sidebar-heading"><span>SpringBoot系列</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-2"><p class="sidebar-heading"><span>Mybatis系列</span> <span class="arrow right"></span></p> <!----></section></li></ul></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>redis系列</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>消息队列系列</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>zookeeper系列</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable is-sub-group depth-1"><p class="sidebar-heading"><span>Linux系列</span> <span class="arrow right"></span></p> <!----></section></li></ul></section></li></ul> </aside> <div class="password-shadow password-wrapper-in" style="display:none;" data-v-4e82dffc data-v-1156296a><h3 class="title" data-v-4e82dffc data-v-4e82dffc>Spring夺命连环10问</h3> <!----> <label id="box" class="inputBox" data-v-4e82dffc data-v-4e82dffc><input type="password" value="" data-v-4e82dffc> <span data-v-4e82dffc>Konck! Knock!</span> <button data-v-4e82dffc>OK</button></label> <div class="footer" data-v-4e82dffc data-v-4e82dffc><span data-v-4e82dffc><i class="iconfont reco-theme" data-v-4e82dffc></i> <a target="blank" href="https://vuepress-theme-reco.recoluan.com" data-v-4e82dffc>vuePress-theme-reco</a></span> <span data-v-4e82dffc><i class="iconfont reco-copyright" data-v-4e82dffc></i> <a data-v-4e82dffc><span data-v-4e82dffc>小狐</span>
            
          <span data-v-4e82dffc>2021-9-1 - </span>
          2021
        </a></span></div></div> <div data-v-1156296a><main class="page"><section><div class="page-title"><h1 class="title">Spring夺命连环10问</h1> <div data-v-1ff7123e><i class="iconfont reco-account" data-v-1ff7123e><span data-v-1ff7123e>小狐</span></i> <i class="iconfont reco-date" data-v-1ff7123e><span data-v-1ff7123e>10/1/2021</span></i> <!----> <i class="tags iconfont reco-tag" data-v-1ff7123e><span class="tag-item" data-v-1ff7123e>java</span><span class="tag-item" data-v-1ff7123e>Spring</span></i></div></div> <div class="theme-reco-content content__default"><h1 id="spring夺命连环10问"><a href="#spring夺命连环10问" class="header-anchor">#</a> Spring夺命连环10问</h1> <h3 id="_1-说说spring-里用到了哪些设计模式"><a href="#_1-说说spring-里用到了哪些设计模式" class="header-anchor">#</a> 1.说说Spring 里用到了哪些设计模式?</h3> <p><code>单例模式</code>：Spring 中的 Bean 默认情况下都是单例的。无需多说。</p> <p><code>工厂模式</code>：工厂模式主要是通过 BeanFactory 和 ApplicationContext 来生产 Bean 对象。</p> <p><code>代理模式</code>：最常见的 AOP 的实现方式就是通过代理来实现，Spring主要是使用 JDK 动态代理和 CGLIB 代理。</p> <p><code>模板方法模式</code>：主要是一些对数据库操作的类用到，比如 JdbcTemplate、JpaTemplate，因为查询数据库的建立连接、执行查询、关闭连接几个过程，非常适用于模板方法。</p> <h3 id="_2-谈谈你对ioc-和-aop-的理解-他们的实现原理是什么"><a href="#_2-谈谈你对ioc-和-aop-的理解-他们的实现原理是什么" class="header-anchor">#</a> 2.谈谈你对IOC 和 AOP 的理解？他们的实现原理是什么？</h3> <p>IOC 叫做控制反转，指的是通过Spring来管理对象的创建、配置和生命周期，这样相当于把控制权交给了Spring，不需要人工来管理对象之间复杂的依赖关系，这样做的好处就是解耦。在Spring里面，主要提供了 BeanFactory 和 ApplicationContext 两种 IOC 容器，通过他们来实现对 Bean 的管理。</p> <p>AOP 叫做面向切面编程，他是一个编程范式，目的就是提高代码的模块性。Srping AOP 基于动态代理的方式实现，如果是实现了接口的话就会使用 JDK 动态代理，反之则使用 CGLIB 代理，Spring中 AOP 的应用主要体现在 事务、日志、异常处理等方面，通过在代码的前后做一些增强处理，可以实现对业务逻辑的隔离，提高代码的模块化能力，同时也是解耦。Spring主要提供了 Aspect 切面、JoinPoint 连接点、PointCut 切入点、Advice 增强等实现方式。</p> <h3 id="_3-jdk-动态代理和-cglib-代理有什么区别"><a href="#_3-jdk-动态代理和-cglib-代理有什么区别" class="header-anchor">#</a> 3. JDK 动态代理和 CGLIB 代理有什么区别？</h3> <p>JDK 动态代理主要是针对类实现了某个接口，AOP 则会使用 JDK 动态代理。他基于反射的机制实现，生成一个实现同样接口的一个代理类，然后通过重写方法的方式，实现对代码的增强。</p> <p>而如果某个类没有实现接口，AOP 则会使用 CGLIB 代理。他的底层原理是基于 asm 第三方框架，通过修改字节码生成成成一个子类，然后重写父类的方法，实现对代码的增强。</p> <h3 id="_4-spring-aop-和-aspectj-aop-有什么区别"><a href="#_4-spring-aop-和-aspectj-aop-有什么区别" class="header-anchor">#</a> 4. Spring AOP 和 AspectJ AOP 有什么区别？</h3> <p>Spring AOP 基于动态代理实现，属于运行时增强。</p> <p>AspectJ 则属于编译时增强，主要有3种方式：</p> <ol><li>编译时织入：指的是增强的代码和源代码我们都有，直接使用 AspectJ 编译器编译就行了，编译之后生成一个新的类，他也会作为一个正常的 Java 类装载到JVM。</li> <li>编译后织入：指的是代码已经被编译成 class 文件或者已经打成 jar 包，这时候要增强的话，就是编译后织入，比如你依赖了第三方的类库，又想对他增强的话，就可以通过这种方式。</li></ol> <p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/ibBMVuDfkZUnmFsqZZ4gkrIRbV5o351H2OnmhLVnP2Wys2NkcUoQwQaq6FibWjXwtRgWFBxw0p42baia28SZ1RhibA/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p> <ol><li>加载时织入：指的是在 JVM 加载类的时候进行织入。</li></ol> <p>总结下来的话，就是 Spring AOP 只能在运行时织入，不需要单独编译，性能相比 AspectJ 编译织入的方式慢，而 AspectJ 只支持编译前后和类加载时织入，性能更好，功能更加强大。</p> <h3 id="_5-factorybean-和-beanfactory有什么区别"><a href="#_5-factorybean-和-beanfactory有什么区别" class="header-anchor">#</a> 5. FactoryBean 和 BeanFactory有什么区别？</h3> <p>BeanFactory 是 Bean 的工厂， ApplicationContext 的父类，IOC 容器的核心，负责生产和管理 Bean 对象。</p> <p>FactoryBean 是 Bean，可以通过实现 FactoryBean 接口定制实例化 Bean 的逻辑，通过代理一个Bean对象，对方法前后做一些操作。</p> <h3 id="_6-springbean的生命周期说说"><a href="#_6-springbean的生命周期说说" class="header-anchor">#</a> 6.SpringBean的生命周期说说？</h3> <p>SpringBean 生命周期简单概括为4个阶段：</p> <ol><li><p>实例化，创建一个Bean对象</p></li> <li><p>填充属性，为属性赋值</p></li> <li><p>初始化</p></li> <li><ul><li>如果实现了<code>xxxAware</code>接口，通过不同类型的Aware接口拿到Spring容器的资源</li></ul></li></ol> <ul><li>如果实现了BeanPostProcessor接口，则会回调该接口的<code>postProcessBeforeInitialzation</code>和<code>postProcessAfterInitialization</code>方法</li> <li>如果配置了<code>init-method</code>方法，则会执行<code>init-method</code>配置的方法</li></ul> <ol start="5"><li><p>销毁</p></li> <li><ul><li>容器关闭后，如果Bean实现了<code>DisposableBean</code>接口，则会回调该接口的<code>destroy</code>方法</li></ul></li></ol> <ul><li>如果配置了<code>destroy-method</code>方法，则会执行<code>destroy-method</code>配置的方法</li></ul> <p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/ibBMVuDfkZUnmFsqZZ4gkrIRbV5o351H2AzW6WIZjiaXWE6LUS5J66vgqhicU60FNxIf0V0mv8QqssYniaFBoFibraw/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p> <h3 id="_7-spring是怎么解决循环依赖的"><a href="#_7-spring是怎么解决循环依赖的" class="header-anchor">#</a> 7.Spring是怎么解决循环依赖的？</h3> <p>首先，Spring 解决循环依赖有两个前提条件：</p> <ol><li>不全是构造器方式的循环依赖</li> <li>必须是单例</li></ol> <p>基于上面的问题，我们知道Bean的生命周期，本质上解决循环依赖的问题就是三级缓存，通过三级缓存提前拿到未初始化的对象。</p> <p>第一级缓存：用来保存实例化、初始化都完成的对象</p> <p>第二级缓存：用来保存实例化完成，但是未初始化完成的对象</p> <p>第三级缓存：用来保存一个对象工厂，提供一个匿名内部类，用于创建二级缓存中的对象</p> <p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/ibBMVuDfkZUnmFsqZZ4gkrIRbV5o351H2AKQpf8yvnt5PXLsbCR8bLyMGRDEAslaMMl3AhGJq8bkMUNETTh5qpQ/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p> <p>假设一个简单的循环依赖场景，A、B互相依赖。</p> <p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/ibBMVuDfkZUnmFsqZZ4gkrIRbV5o351H2MNTicM1Yr2ZicnqkgNZky17S90C9icBc9yHzIowluMykeHibvhNsOTkia0Q/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p> <p>A对象的创建过程：</p> <ol><li>创建对象A，实例化的时候把A对象工厂放入三级缓存</li></ol> <p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/ibBMVuDfkZUnmFsqZZ4gkrIRbV5o351H2ic0qR8shPvuk9mib9YEvBqtb4iasibz7Bk1pyZR2HbichnN4LVpK3Y1pMCA/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p> <ol><li>A注入属性时，发现依赖B，转而去实例化B</li> <li>同样创建对象B，注入属性时发现依赖A，一次从一级到三级缓存查询A，从三级缓存通过对象工厂拿到A，把A放入二级缓存，同时删除三级缓存中的A，此时，B已经实例化并且初始化完成，把B放入一级缓存。</li></ol> <p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/ibBMVuDfkZUnmFsqZZ4gkrIRbV5o351H2AIXA8Ic8iaSIBst74uO1cib8wrbAfQpc3icONdicnK8yvialmlLJicUEuWXg/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p> <ol><li>接着继续创建A，顺利从一级缓存拿到实例化且初始化完成的B对象，A对象创建也完成，删除二级缓存中的A，同时把A放入一级缓存</li> <li>最后，一级缓存中保存着实例化、初始化都完成的A、B对象</li></ol> <p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/ibBMVuDfkZUnmFsqZZ4gkrIRbV5o351H24oPiaNGtpjbUhSxW4HNlPqMEFg10eaLU0V7gfrFmHJRv25pwic9XS7JQ/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p> <p>因此，由于把实例化和初始化的流程分开了，所以如果都是用构造器的话，就没法分离这个操作，所以都是构造器的话就无法解决循环依赖的问题了。</p> <h3 id="_8-为什么要三级缓存-二级不行吗"><a href="#_8-为什么要三级缓存-二级不行吗" class="header-anchor">#</a> 8. 为什么要三级缓存？二级不行吗？</h3> <p>不可以，主要是为了生成代理对象。</p> <p>因为三级缓存中放的是生成具体对象的匿名内部类，他可以生成代理对象，也可以是普通的实例对象。</p> <p>使用三级缓存主要是为了保证不管什么时候使用的都是一个对象。</p> <p>假设只有二级缓存的情况，往二级缓存中放的显示一个普通的Bean对象，<code>BeanPostProcessor</code>去生成代理对象之后，覆盖掉二级缓存中的普通Bean对象，那么多线程环境下可能取到的对象就不一致了。</p> <p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/ibBMVuDfkZUnmFsqZZ4gkrIRbV5o351H2tDZJooDFwPAn5JMKHpeLrLDtgkItbZtDpWf9cBVLXFWiaqSO2TYIrEQ/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片"></p> <h3 id="_9-spring事务传播机制有哪些"><a href="#_9-spring事务传播机制有哪些" class="header-anchor">#</a> 9.Spring事务传播机制有哪些？</h3> <ol><li><strong>PROPAGATION_REQUIRED</strong>：如果当前没有事务，就创建一个新事务，如果当前存在事务，就加入该事务，这也是通常我们的默认选择。</li> <li><strong>PROPAGATION_REQUIRES_NEW</strong>：创建新事务，无论当前存不存在事务，都创建新事务。</li> <li>PROPAGATION_NESTED：如果当前存在事务，则在嵌套事务内执行。如果当前没有事务，则按REQUIRED属性执行。</li> <li>PROPAGATION_NOT_SUPPORTED：以非事务方式执行操作，如果当前存在事务，就把当前事务挂起。</li> <li>PROPAGATION_NEVER：以非事务方式执行，如果当前存在事务，则抛出异常。</li> <li>PROPAGATION_MANDATORY：支持当前事务，如果当前存在事务，就加入该事务，如果当前不存在事务，就抛出异常。</li> <li>PROPAGATION_SUPPORTS：支持当前事务，如果当前存在事务，就加入该事务，如果当前不存在事务，就以非事务执行。‘</li></ol> <h3 id="_10-最后-说说spring-boot-启动流程吧"><a href="#_10-最后-说说spring-boot-启动流程吧" class="header-anchor">#</a> 10.最后，说说Spring Boot 启动流程吧？</h3> <p>这个流程，网上一搜基本都是这张图了，我也不想再画一遍了。那其实主要的流程就几个步骤：</p> <ol><li>准备环境，根据不同的环境创建不同的Environment</li> <li>准备、加载上下文，为不同的环境选择不同的Spring Context，然后加载资源，配置Bean</li> <li>初始化，这个阶段刷新Spring Context，启动应用</li> <li>最后结束流程</li></ol> <p><img src="https://mmbiz.qpic.cn/mmbiz_jpg/ibBMVuDfkZUnmFsqZZ4gkrIRbV5o351H2yht9fh2bYQ6zDibebR46AiaKz50jvicw2L6sXNz79CNrCbrpnibxw4QCJg/640?wx_fmt=jpeg&amp;tp=webp&amp;wxfrom=5&amp;wx_lazy=1&amp;wx_co=1" alt="图片">图片来源网络，如有侵权请联系我删除</p> <p>原文地址:https://mp.weixin.qq.com/s/ovLO8gn_0GFrNFIii6niKw</p></div></section> <footer class="page-edit"><!----> <div class="last-updated"><span class="prefix">2021-9-2: </span> <span class="time">9/3/2021, 6:20:18 PM</span></div></footer> <div class="page-nav"><p class="inner"><span class="prev"><a href="/blogs/audition/java/spring/spring_base.html" class="prev">
            Spring基础面试题
          </a></span> <span class="next"><a href="/blogs/audition/java/springboot/springboot_base.html">
            非常基础的22道SpringBoot面试题
          </a></span></p></div> <div class="comments-wrapper"><!----></div> <ul class="side-bar sub-sidebar-wrapper" style="width:12rem;" data-v-70334359><li class="level-3" data-v-70334359><a href="/blogs/audition/java/spring/spring_advanced_1.html#_1-说说spring-里用到了哪些设计模式" class="sidebar-link reco-side-_1-说说spring-里用到了哪些设计模式" data-v-70334359>1.说说Spring 里用到了哪些设计模式?</a></li><li class="level-3" data-v-70334359><a href="/blogs/audition/java/spring/spring_advanced_1.html#_2-谈谈你对ioc-和-aop-的理解-他们的实现原理是什么" class="sidebar-link reco-side-_2-谈谈你对ioc-和-aop-的理解-他们的实现原理是什么" data-v-70334359>2.谈谈你对IOC 和 AOP 的理解？他们的实现原理是什么？</a></li><li class="level-3" data-v-70334359><a href="/blogs/audition/java/spring/spring_advanced_1.html#_3-jdk-动态代理和-cglib-代理有什么区别" class="sidebar-link reco-side-_3-jdk-动态代理和-cglib-代理有什么区别" data-v-70334359>3. JDK 动态代理和 CGLIB 代理有什么区别？</a></li><li class="level-3" data-v-70334359><a href="/blogs/audition/java/spring/spring_advanced_1.html#_4-spring-aop-和-aspectj-aop-有什么区别" class="sidebar-link reco-side-_4-spring-aop-和-aspectj-aop-有什么区别" data-v-70334359>4. Spring AOP 和 AspectJ AOP 有什么区别？</a></li><li class="level-3" data-v-70334359><a href="/blogs/audition/java/spring/spring_advanced_1.html#_5-factorybean-和-beanfactory有什么区别" class="sidebar-link reco-side-_5-factorybean-和-beanfactory有什么区别" data-v-70334359>5. FactoryBean 和 BeanFactory有什么区别？</a></li><li class="level-3" data-v-70334359><a href="/blogs/audition/java/spring/spring_advanced_1.html#_6-springbean的生命周期说说" class="sidebar-link reco-side-_6-springbean的生命周期说说" data-v-70334359>6.SpringBean的生命周期说说？</a></li><li class="level-3" data-v-70334359><a href="/blogs/audition/java/spring/spring_advanced_1.html#_7-spring是怎么解决循环依赖的" class="sidebar-link reco-side-_7-spring是怎么解决循环依赖的" data-v-70334359>7.Spring是怎么解决循环依赖的？</a></li><li class="level-3" data-v-70334359><a href="/blogs/audition/java/spring/spring_advanced_1.html#_8-为什么要三级缓存-二级不行吗" class="sidebar-link reco-side-_8-为什么要三级缓存-二级不行吗" data-v-70334359>8. 为什么要三级缓存？二级不行吗？</a></li><li class="level-3" data-v-70334359><a href="/blogs/audition/java/spring/spring_advanced_1.html#_9-spring事务传播机制有哪些" class="sidebar-link reco-side-_9-spring事务传播机制有哪些" data-v-70334359>9.Spring事务传播机制有哪些？</a></li><li class="level-3" data-v-70334359><a href="/blogs/audition/java/spring/spring_advanced_1.html#_10-最后-说说spring-boot-启动流程吧" class="sidebar-link reco-side-_10-最后-说说spring-boot-启动流程吧" data-v-70334359>10.最后，说说Spring Boot 启动流程吧？</a></li></ul></main> <!----></div></div></div></div><div class="global-ui"><div class="back-to-ceiling" style="right:1rem;bottom:6rem;width:2.5rem;height:2.5rem;border-radius:.25rem;line-height:2.5rem;display:none;" data-v-c6073ba8 data-v-c6073ba8><svg t="1574745035067" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg" p-id="5404" class="icon" data-v-c6073ba8><path d="M526.60727968 10.90185116a27.675 27.675 0 0 0-29.21455937 0c-131.36607665 82.28402758-218.69155461 228.01873535-218.69155402 394.07834331a462.20625001 462.20625001 0 0 0 5.36959153 69.94390903c1.00431239 6.55289093-0.34802892 13.13561351-3.76865779 18.80351572-32.63518765 54.11355614-51.75690182 118.55860487-51.7569018 187.94566865a371.06718723 371.06718723 0 0 0 11.50484808 91.98906777c6.53300375 25.50556257 41.68394495 28.14064038 52.69160883 4.22606766 17.37162448-37.73630017 42.14135425-72.50938081 72.80769204-103.21549295 2.18761121 3.04276886 4.15646224 6.24463696 6.40373557 9.22774369a1871.4375 1871.4375 0 0 0 140.04691725 5.34970492 1866.36093723 1866.36093723 0 0 0 140.04691723-5.34970492c2.24727335-2.98310674 4.21612437-6.18497483 6.3937923-9.2178004 30.66633723 30.70611158 55.4360664 65.4791928 72.80769147 103.21549355 11.00766384 23.91457269 46.15860503 21.27949489 52.69160879-4.22606768a371.15156223 371.15156223 0 0 0 11.514792-91.99901164c0-69.36717486-19.13165746-133.82216804-51.75690182-187.92578088-3.42062944-5.66790279-4.76302748-12.26056868-3.76865837-18.80351632a462.20625001 462.20625001 0 0 0 5.36959269-69.943909c-0.00994388-166.08943902-87.32547796-311.81420293-218.6915546-394.09823051zM605.93803103 357.87693858a93.93749974 93.93749974 0 1 1-187.89594924 6.1e-7 93.93749974 93.93749974 0 0 1 187.89594924-6.1e-7z" p-id="5405" data-v-c6073ba8></path><path d="M429.50777625 765.63860547C429.50777625 803.39355007 466.44236686 1000.39046097 512.00932183 1000.39046097c45.56695499 0 82.4922232-197.00623328 82.5015456-234.7518555 0-37.75494459-36.9345906-68.35043303-82.4922232-68.34111062-45.57627738-0.00932239-82.52019037 30.59548842-82.51086798 68.34111062z" p-id="5406" data-v-c6073ba8></path></svg></div><!----><canvas id="vuepress-canvas-cursor"></canvas></div></div>
    <script src="/assets/js/app.7029ddab.js" defer></script><script src="/assets/js/3.ebaa3085.js" defer></script><script src="/assets/js/1.8ce67e8c.js" defer></script><script src="/assets/js/61.0b61afdb.js" defer></script>
  </body>
</html>
